package com.example.springlearn.listener;

import com.example.springlearn.bean.QuestionInfo;
import com.example.springlearn.service.QuestionService;
import com.example.springlearn.util.StaticParamUtil;
import com.example.springlearn.util.redis.ListUtil;
import com.example.springlearn.util.redis.ToolsUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.util.List;

@Slf4j
@Component
public class ApplicationListener implements ServletContextListener {

    @Autowired
    QuestionService questionService;

    @Autowired
    ListUtil listUtil;

    @Resource
    ToolsUtil toolsUtil;

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        boolean b = toolsUtil.hasKey(StaticParamUtil.ALL_QUESTIONS);
        if (b) {
            log.warn("正在重新加载");
            toolsUtil.del(StaticParamUtil.ALL_QUESTIONS);
        }
        questionService.queryAll();
        ServletContextListener.super.contextInitialized(sce);
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        ServletContextListener.super.contextDestroyed(sce);
    }
}
